<?php
/**
 * Description
 *
 * PHP version 7
 *
 * @category Custom
 * @package  Custom
 * @author   ryu <mo5467@126.com>
 * @date     2020-04-28 01:15:16
 * @lastTime 2020-04-28 01:31:27
 * @license  https://opensource.org/licenses/MIT MIT License
 * @link     none
 */
namespace Test\Unit;

use Design\Behavioral\Iterator\Book;
use Design\Behavioral\Iterator\BookList;

class IteratorTest extends \Test\BaseTest
{


    public function testCanAddBook()
    {
        $bookList = new BookList();
        $bookList->addBook(new Book('book', 'ryu'));

        $this->assertCount(1, $bookList);

    }//end testCanAddBook()


    public function testCanRemoveBook()
    {
        $book     = new Book('book', 'ryu');
        $bookList = new BookList();
        $bookList->addBook($book);
        $bookList->removeBook($book);

        $this->assertCount(0, $bookList);

    }//end testCanRemoveBook()


    public function testCanIteratorBookListAfterRemoveBook()
    {
        $book     = new Book('book', 'ryu');
        $book2    = new Book('book2', 'ryu');
        $bookList = new BookList();
        $bookList->addBook($book);
        $bookList->addBook($book2);
        $bookList->removeBook($book);

        $books = [];
        foreach ($bookList as $_book) {
            $books[] = $_book->getAuthorAndTitle();
        }

        $this->assertSame(['book2 by ryu'], $books);

    }//end testCanIteratorBookListAfterRemoveBook()


    public function testCanIteratorBookList()
    {
        $bookList = new BookList();
        $bookList->addBook(new Book('book1', 'ryu'));
        $bookList->addBook(new Book('book2', 'ryu'));
        $bookList->addBook(new Book('book3', 'ryu'));

        $books = [];
        foreach ($bookList as $key => $book) {
            $books[] = $book->getAuthorAndTitle();
        }

        $this->assertSame(
            [
                'book1 by ryu',
                'book2 by ryu',
                'book3 by ryu',
            ],
            $books
        );

    }//end testCanIteratorBookList()


}//end class
